using System;
using System.Data;
using System.Configuration;
using CodeWork.Library.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlTypes;
using CodeWork.Library;
using System.Text;
using System.Data.SqlClient;

/// <summary>
/// ORM object that maps to tbl_UserProfile
/// </summary>
namespace EasyCommerce.DAL
{
    public class UserProfile : SqlServerDAO
    {
        /// <summary>
        /// Self table parameters
        /// </summary>
        private int _id;
		private string _firstName;
		private string _lastName;
		private int _userAddressId;
		private int _billingAddressId;
		private int _shippingAddressId;
		private string _selectedTheme;
		private int _selectedTimeZone;
		private double _giftCouponBalance;
		private int _profileForSiteuserId;
		private DateTime _dateOfBirth;

        /// <summary>
        /// Foriegn key parameters
        /// </summary>
        

        /// <summary>
        /// Self table properties
        /// </summary>
		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public string FirstName
		{
			get { return this._firstName; }
			set { this._firstName = value; }
		}

		public string LastName
		{
			get { return this._lastName; }
			set { this._lastName = value; }
		}

		public int UserAddressId
		{
			get { return this._userAddressId; }
			set { this._userAddressId = value; }
		}

		public int BillingAddressId
		{
			get { return this._billingAddressId; }
			set { this._billingAddressId = value; }
		}

		public int ShippingAddressId
		{
			get { return this._shippingAddressId; }
			set { this._shippingAddressId = value; }
		}

		public string SelectedTheme
		{
			get { return this._selectedTheme; }
			set { this._selectedTheme = value; }
		}

		public int SelectedTimeZone
		{
			get { return this._selectedTimeZone; }
			set { this._selectedTimeZone = value; }
		}

		public double GiftCouponBalance
		{
			get { return this._giftCouponBalance; }
			set { this._giftCouponBalance = value; }
		}

		public int ProfileForSiteuserId
		{
			get { return this._profileForSiteuserId; }
			set { this._profileForSiteuserId = value; }
		}

		public DateTime DateOfBirth
		{
			get { return this._dateOfBirth; }
			set { this._dateOfBirth = value; }
		}
		
        /// <summary>
        /// Foreign key properties, loaded on demand only
        /// </summary>
        

        /// <summary>
        /// Empty Constructor
        /// </summary>
        public UserProfile() { }

        /// <summary>
        /// Parameterized constructor
        /// </summary>
        /// <param name="id"></param>
		/// <param name="firstName"></param>
		/// <param name="lastName"></param>
		/// <param name="userAddressId"></param>
		/// <param name="billingAddressId"></param>
		/// <param name="shippingAddressId"></param>
		/// <param name="selectedTheme"></param>
		/// <param name="selectedTimeZone"></param>
		/// <param name="giftCouponBalance"></param>
		/// <param name="profileForSiteuserId"></param>
		/// <param name="dateOfBirth"></param>
        public UserProfile
        (
            int id,
			string firstName,
			string lastName,
			int userAddressId,
			int billingAddressId,
			int shippingAddressId,
			string selectedTheme,
			int selectedTimeZone,
			double giftCouponBalance,
			int profileForSiteuserId,
			DateTime dateOfBirth
        )
        {
            this._id = id;
			this._firstName = firstName;
			this._lastName = lastName;
			this._userAddressId = userAddressId;
			this._billingAddressId = billingAddressId;
			this._shippingAddressId = shippingAddressId;
			this._selectedTheme = selectedTheme;
			this._selectedTimeZone = selectedTimeZone;
			this._giftCouponBalance = giftCouponBalance;
			this._profileForSiteuserId = profileForSiteuserId;
			this._dateOfBirth = dateOfBirth;
        }

        /// <summary>
        /// Static save method. Saves the current instance in the table using the provided values using the
        /// primary key id. Expects that the Id is a primary key of an existing record in table.
        /// </summary>
        /// <param name="id"></param>
		/// <param name="firstName"></param>
		/// <param name="lastName"></param>
		/// <param name="userAddressId"></param>
		/// <param name="billingAddressId"></param>
		/// <param name="shippingAddressId"></param>
		/// <param name="selectedTheme"></param>
		/// <param name="selectedTimeZone"></param>
		/// <param name="giftCouponBalance"></param>
		/// <param name="profileForSiteuserId"></param>
		/// <param name="dateOfBirth"></param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool Save
        (
            int id,
			string firstName,
			string lastName,
			int userAddressId,
			int billingAddressId,
			int shippingAddressId,
			string selectedTheme,
			int selectedTimeZone,
			double giftCouponBalance,
			int profileForSiteuserId,
			DateTime dateOfBirth
        )
        {
            UserProfile userProfile = new UserProfile();
            userProfile.Id = id;
			userProfile.FirstName = firstName;
			userProfile.LastName = lastName;
			userProfile.UserAddressId = userAddressId;
			userProfile.BillingAddressId = billingAddressId;
			userProfile.ShippingAddressId = shippingAddressId;
			userProfile.SelectedTheme = selectedTheme;
			userProfile.SelectedTimeZone = selectedTimeZone;
			userProfile.GiftCouponBalance = giftCouponBalance;
			userProfile.ProfileForSiteuserId = profileForSiteuserId;
			userProfile.DateOfBirth = dateOfBirth;

            return userProfile.Save();
        }

        /// <summary>
        /// Saves the current instance and returns result
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Save()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);
			AddCommandParameter(command, "@FirstName", SqlDbType.NVarChar, 250, ParameterDirection.Input, FirstName);
			AddCommandParameter(command, "@LastName", SqlDbType.NVarChar, 250, ParameterDirection.Input, LastName);
			AddCommandParameter(command, "@UserAddressId", SqlDbType.Int, 0, ParameterDirection.Input, UserAddressId);
			AddCommandParameter(command, "@BillingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, BillingAddressId);
			AddCommandParameter(command, "@ShippingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, ShippingAddressId);
			AddCommandParameter(command, "@SelectedTheme", SqlDbType.NVarChar, 50, ParameterDirection.Input, SelectedTheme);
			AddCommandParameter(command, "@SelectedTimeZone", SqlDbType.Int, 0, ParameterDirection.Input, SelectedTimeZone);
			AddCommandParameter(command, "@GiftCouponBalance", SqlDbType.Decimal, 0, ParameterDirection.Input, GiftCouponBalance);
			AddCommandParameter(command, "@ProfileForSiteuserId", SqlDbType.Int, 0, ParameterDirection.Input, ProfileForSiteuserId);
			AddCommandParameter(command, "@DateOfBirth", SqlDbType.DateTime, 0, ParameterDirection.Input, DateOfBirth);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_Save.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Saves the current instance based on provided conditions. This method assumes that
        /// conditions are provided in correct order and in proper format.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool Save(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@NewFirstName", SqlDbType.NVarChar, 250, ParameterDirection.Input, FirstName);
			AddCommandParameter(command, "@NewLastName", SqlDbType.NVarChar, 250, ParameterDirection.Input, LastName);
			AddCommandParameter(command, "@NewUserAddressId", SqlDbType.Int, 0, ParameterDirection.Input, UserAddressId);
			AddCommandParameter(command, "@NewBillingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, BillingAddressId);
			AddCommandParameter(command, "@NewShippingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, ShippingAddressId);
			AddCommandParameter(command, "@NewSelectedTheme", SqlDbType.NVarChar, 50, ParameterDirection.Input, SelectedTheme);
			AddCommandParameter(command, "@NewSelectedTimeZone", SqlDbType.Int, 0, ParameterDirection.Input, SelectedTimeZone);
			AddCommandParameter(command, "@NewGiftCouponBalance", SqlDbType.Decimal, 0, ParameterDirection.Input, GiftCouponBalance);
			AddCommandParameter(command, "@NewProfileForSiteuserId", SqlDbType.Int, 0, ParameterDirection.Input, ProfileForSiteuserId);
			AddCommandParameter(command, "@NewDateOfBirth", SqlDbType.DateTime, 0, ParameterDirection.Input, DateOfBirth);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_SaveConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static creational method. This method actually adds a new entry in the table with
        /// the provided information.
        /// </summary>
        /// <param name="firstName"></param>
		/// <param name="lastName"></param>
		/// <param name="userAddressId"></param>
		/// <param name="billingAddressId"></param>
		/// <param name="shippingAddressId"></param>
		/// <param name="selectedTheme"></param>
		/// <param name="selectedTimeZone"></param>
		/// <param name="giftCouponBalance"></param>
		/// <param name="profileForSiteuserId"></param>
		/// <param name="dateOfBirth"></param>
        /// <returns>Instance of the created object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static UserProfile Create
        (
            string firstName,
			string lastName,
			int userAddressId,
			int billingAddressId,
			int shippingAddressId,
			string selectedTheme,
			int selectedTimeZone,
			double giftCouponBalance,
			int profileForSiteuserId,
			DateTime dateOfBirth
        )
        {
            UserProfile userProfile = new UserProfile();
            userProfile.FirstName = firstName;
			userProfile.LastName = lastName;
			userProfile.UserAddressId = userAddressId;
			userProfile.BillingAddressId = billingAddressId;
			userProfile.ShippingAddressId = shippingAddressId;
			userProfile.SelectedTheme = selectedTheme;
			userProfile.SelectedTimeZone = selectedTimeZone;
			userProfile.GiftCouponBalance = giftCouponBalance;
			userProfile.ProfileForSiteuserId = profileForSiteuserId;
			userProfile.DateOfBirth = dateOfBirth;

            return (userProfile.Create() ? userProfile : null);
        }

        /// <summary>
        /// Instance creational method. Inserts the current object as a new entry in table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Create()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@FirstName", SqlDbType.NVarChar, 250, ParameterDirection.Input, FirstName);
			AddCommandParameter(command, "@LastName", SqlDbType.NVarChar, 250, ParameterDirection.Input, LastName);
			AddCommandParameter(command, "@UserAddressId", SqlDbType.Int, 0, ParameterDirection.Input, UserAddressId);
			AddCommandParameter(command, "@BillingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, BillingAddressId);
			AddCommandParameter(command, "@ShippingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, ShippingAddressId);
			AddCommandParameter(command, "@SelectedTheme", SqlDbType.NVarChar, 50, ParameterDirection.Input, SelectedTheme);
			AddCommandParameter(command, "@SelectedTimeZone", SqlDbType.Int, 0, ParameterDirection.Input, SelectedTimeZone);
			AddCommandParameter(command, "@GiftCouponBalance", SqlDbType.Decimal, 0, ParameterDirection.Input, GiftCouponBalance);
			AddCommandParameter(command, "@ProfileForSiteuserId", SqlDbType.Int, 0, ParameterDirection.Input, ProfileForSiteuserId);
			AddCommandParameter(command, "@DateOfBirth", SqlDbType.DateTime, 0, ParameterDirection.Input, DateOfBirth);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_Create.ToString());

            Id = (int)GetOneData(command);
            return (Id == 0 ? false : true);
        }

        /// <summary>
        /// Static deletion method. Deletes a record from table using the provided primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(int id)
        {
            UserProfile userProfile = new UserProfile();
            userProfile.Id = id;

            return userProfile.Delete();
        }

        /// <summary>
        /// Static deletion method. Deletes a list record from table using the provided primary keys id.
        /// </summary>
        /// <param name="ids">List of primarky key id</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(List<int> ids)
        {
            string csvIds = Utility.MakeCSV(ids);

            UserProfile userProfile = new UserProfile();

            return userProfile.Delete(csvIds);
        }

        /// <summary>
        /// Static deletion method. Deletes records from table using provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        public static bool Delete(List<Condition> conditions)
        {
            UserProfile userProfile = new UserProfile();

            return userProfile.ConditionalDelete(conditions);
        }

        /// <summary>
        /// This is helper method for conditional delete method above. Not for other general use.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool ConditionalDelete(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_DeleteConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// This is a helper method for multiple id delete method. Not for general use.
        /// </summary>
        /// <param name="csvIds">Comma seperated primary key values as string</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool Delete(string csvIds)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Ids", SqlDbType.NVarChar, 500, ParameterDirection.Input, csvIds);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_DeleteMultiple.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Instance deletion method. Deletes the current instance from table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Delete()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_DeleteOne.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static selection method. Used for loading a perticular data using primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Instance of the loaded object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static UserProfile Load(int id)
        {
            UserProfile userProfile = new UserProfile();
            userProfile.Id = id;

            if (userProfile.Load())
                return userProfile;
            else
                return null;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting and also with 
        /// custom conditions.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<UserProfile> Load(string orderBy, int startRow, int pageSize, List<Condition> conditions)
        {
            List<UserProfile> userProfiles = new List<UserProfile>();
			
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_LoadConditionalPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                UserProfile userProfile = (UserProfile)data;
                userProfiles.Add(userProfile);
            }

            return userProfiles;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Count of the records</returns>
        public int LoadCount(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_LoadConditionalPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<UserProfile> Load(string orderBy, int startRow, int pageSize)
        {
            List<UserProfile> userProfiles = new List<UserProfile>();
            
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_LoadPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                UserProfile userProfile = (UserProfile)data;
                userProfiles.Add(userProfile);
            }

            return userProfiles;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <returns>Count of the records</returns>
        public int LoadCount()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_LoadPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method. Used for reloading the current object from table. This method uses
        /// the Id of the current object to load and expects that the Id is correct.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Load()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_Load.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                UserProfile userProfile = (UserProfile)data;

                this._id = userProfile._id;
				this._firstName = userProfile._firstName;
				this._lastName = userProfile._lastName;
				this._userAddressId = userProfile._userAddressId;
				this._billingAddressId = userProfile._billingAddressId;
				this._shippingAddressId = userProfile._shippingAddressId;
				this._selectedTheme = userProfile._selectedTheme;
				this._selectedTimeZone = userProfile._selectedTimeZone;
				this._giftCouponBalance = userProfile._giftCouponBalance;
				this._profileForSiteuserId = userProfile._profileForSiteuserId;
				this._dateOfBirth = userProfile._dateOfBirth;

                break;
            }

            return (dataList.Count > 0 ? true : false);
        }

        /// <summary>
        /// Instance selection method. Used for loading multiple record based on provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<UserProfile> Load(List<Condition> conditions)
        {
            List<UserProfile> userProfiles = new List<UserProfile>();

			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_UserProfile_LoadConditional.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                UserProfile userProfile = (UserProfile)data;
                userProfiles.Add(userProfile);
            }

            return userProfiles;
        }

        /// <summary>
        /// Prepares the conditional parameters for use with Create/ Save/ Load/ Delete methods
        /// that use custom condition. Prepares the user Command object for using conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        private void PrepareConditionalParameters(SqlCommand command, List<Condition> conditions)
        {
            StringBuilder condition = new StringBuilder();

            object id = null;
			object firstName = null;
			object lastName = null;
			object userAddressId = null;
			object billingAddressId = null;
			object shippingAddressId = null;
			object selectedTheme = null;
			object selectedTimeZone = null;
			object giftCouponBalance = null;
			object profileForSiteuserId = null;
			object dateOfBirth = null;

            for (int i = 0; i < conditions.Count; i++)
            {
                string fieldName = Utility.ToHungarianNotation(conditions[i].FieldName);

                if (conditions[i].JoiningCondition == JoinOperator.FirstValue)
                {
                    condition.Append("WHERE ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.And)
                {
                    condition.Append(" AND ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.Or)
                {
                    condition.Append(" OR ");
                }

                condition.Append(conditions[i].FieldName).Append(" ").Append(Condition.MapCondition(conditions[i].ConditionalOperator)).Append("@" + fieldName);

                if ((conditions[i].ConditionalOperator == ConditionOperator.Like ||
                    conditions[i].ConditionalOperator == ConditionOperator.NotLike))
                {
                    if (string.IsNullOrEmpty(conditions[i].Value.ToString()))
                        conditions[i].Value = "%%%";
                    else
                        conditions[i].Value = "%" + conditions[i].Value.ToString() + "%";
                }

                if (fieldName == TableColumns.Id.ToString())
                    id = conditions[i].Value;
                else if (fieldName == TableColumns.FirstName.ToString())
					firstName = conditions[i].Value;
				else if (fieldName == TableColumns.LastName.ToString())
					lastName = conditions[i].Value;
				else if (fieldName == TableColumns.UserAddressId.ToString())
					userAddressId = conditions[i].Value;
				else if (fieldName == TableColumns.BillingAddressId.ToString())
					billingAddressId = conditions[i].Value;
				else if (fieldName == TableColumns.ShippingAddressId.ToString())
					shippingAddressId = conditions[i].Value;
				else if (fieldName == TableColumns.SelectedTheme.ToString())
					selectedTheme = conditions[i].Value;
				else if (fieldName == TableColumns.SelectedTimeZone.ToString())
					selectedTimeZone = conditions[i].Value;
				else if (fieldName == TableColumns.GiftCouponBalance.ToString())
					giftCouponBalance = conditions[i].Value;
				else if (fieldName == TableColumns.ProfileForSiteuserId.ToString())
					profileForSiteuserId = conditions[i].Value;
				else if (fieldName == TableColumns.DateOfBirth.ToString())
					dateOfBirth = conditions[i].Value;
            }

            AddCommandParameter(command, "@Condition", SqlDbType.NVarChar, 2000, ParameterDirection.Input, condition.ToString());
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
			AddCommandParameter(command, "@FirstName", SqlDbType.NVarChar, 250, ParameterDirection.Input, firstName);
			AddCommandParameter(command, "@LastName", SqlDbType.NVarChar, 250, ParameterDirection.Input, lastName);
			AddCommandParameter(command, "@UserAddressId", SqlDbType.Int, 0, ParameterDirection.Input, userAddressId);
			AddCommandParameter(command, "@BillingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, billingAddressId);
			AddCommandParameter(command, "@ShippingAddressId", SqlDbType.Int, 0, ParameterDirection.Input, shippingAddressId);
			AddCommandParameter(command, "@SelectedTheme", SqlDbType.NVarChar, 50, ParameterDirection.Input, selectedTheme);
			AddCommandParameter(command, "@SelectedTimeZone", SqlDbType.Int, 0, ParameterDirection.Input, selectedTimeZone);
			AddCommandParameter(command, "@GiftCouponBalance", SqlDbType.Decimal, 0, ParameterDirection.Input, giftCouponBalance);
			AddCommandParameter(command, "@ProfileForSiteuserId", SqlDbType.Int, 0, ParameterDirection.Input, profileForSiteuserId);
			AddCommandParameter(command, "@DateOfBirth", SqlDbType.DateTime, 0, ParameterDirection.Input, dateOfBirth);
        }

        /// <summary>
        /// Binds the fetched data from data reader to Objects
        /// </summary>
        /// <param name="reader">Data reader supplied by BaseDAO</param>
        /// <returns>List of object instances that was bound</returns>
        protected override List<BaseDAO> BindData(DbDataReader reader)
        {
            List<BaseDAO> result = new List<BaseDAO>();

			if (reader == null)
                return null;

            while (reader.Read())
            {
                UserProfile userProfile = new UserProfile
                (
					reader["id"] == System.DBNull.Value ? GetIdMinValue : (int)reader["id"],
                    reader["first_name"] == System.DBNull.Value ? string.Empty : (string)reader["first_name"],
					reader["last_name"] == System.DBNull.Value ? string.Empty : (string)reader["last_name"],
					reader["user_address_id"] == System.DBNull.Value ? 0 : (int)reader["user_address_id"],
					reader["billing_address_id"] == System.DBNull.Value ? 0 : (int)reader["billing_address_id"],
					reader["shipping_address_id"] == System.DBNull.Value ? 0 : (int)reader["shipping_address_id"],
					reader["selected_theme"] == System.DBNull.Value ? string.Empty : (string)reader["selected_theme"],
					reader["selected_time_zone"] == System.DBNull.Value ? 0 : (int)reader["selected_time_zone"],
					reader["gift_coupon_balance"] == System.DBNull.Value ? 0.00 : (double)reader["gift_coupon_balance"],
					reader["profile_for_siteuser_id"] == System.DBNull.Value ? 0 : (int)reader["profile_for_siteuser_id"],
					reader["date_of_birth"] == System.DBNull.Value ? SqlDateTime.MinValue.Value : (DateTime)reader["date_of_birth"]
                );

                result.Add(userProfile);
            }
            
            if(!reader.IsClosed)
                reader.Close();

            return result;
        }

        /// <summary>
        /// List of store procedures that this class use
        /// </summary>
        private enum StoreProcedureName
        {
            Sp_UserProfile_Save = 0,
            Sp_UserProfile_Create = 1,
            Sp_UserProfile_DeleteOne = 2,
            Sp_UserProfile_DeleteMultiple = 3,
            Sp_UserProfile_DeleteConditional = 4,
            Sp_UserProfile_SaveConditional = 5,
            Sp_UserProfile_LoadConditional = 6,
            Sp_UserProfile_Load = 7,
            Sp_UserProfile_LoadConditionalPaged = 8,
            Sp_UserProfile_LoadConditionalPagedCount = 9,
            Sp_UserProfile_LoadPaged = 10,
            Sp_UserProfile_LoadPagedCount = 11
        }

        /// <summary>
        /// List of table column names that this class use
        /// </summary>
        private enum TableColumns
        {
            Id = 0,
			FirstName = 1,
			LastName = 2,
			UserAddressId = 3,
			BillingAddressId = 4,
			ShippingAddressId = 5,
			SelectedTheme = 6,
			SelectedTimeZone = 7,
			GiftCouponBalance = 8,
			ProfileForSiteuserId = 9,
			DateOfBirth = 10
        }
    }
}